<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '备料单'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
    >
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <div
            class="flex flex-a-c"
            v-for="(item, index) in model.shelfGoodsNumList"
            :key="index"
          >
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="库位"
              hasFeedback
            >
              <a-input v-model.trim="item.shelfName" disabled />
            </a-form-model-item>
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="库位数量"
              hasFeedback
            >
              <a-input v-model.trim="item.maximumQuantity" disabled />
            </a-form-model-item>
            <a-form-model-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              label="备料数量"
              hasFeedback
              :rules="rules.quantity"
              :prop="`shelfGoodsNumList[${index}].quantity`"
            >
              <a-input-number
                style="width:100%"
                v-model.trim="item.quantity"
                :min="0"
                :max="item.maximumQuantity"
                placeholder="请输入数量"
              />
            </a-form-model-item>
          </div>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";

export default {
  props: {
    treeData: {
      type: Array,
    },
  },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 1000, // 弹框宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
    };
  },
  computed: {
    rules() {
      return {
        quantity: [
          { required: true, message: "请输入数量", trigger: "change" },
        ],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit(this.model);
    },

    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.$set(this.model, "goodsId", record.goodsId);
      if (record.shelfGoodsNumList && record.shelfGoodsNumList.length > 0) {
        this.$set(this.model, "shelfGoodsNumList", record.shelfGoodsNumList);
      } else {
        this.getSelShelfGoodsNum(record.goodsId);
      }
    },
    async getSelShelfGoodsNum(goodsId) {
      const res = await api.SelShelfGoodsNum({ goodsId });
      let arr = res.data.map((item) => {
        return {
          id: item.id,
          maximumQuantity: item.quantity,
          quantity: 0,
          shelfName: item.shelfName,
          goodsId: item.goodsId,
        };
      });
      this.$set(this.model, "shelfGoodsNumList", arr);
    },
    handleOk() {
      const that = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.handleSubmit();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
      this.model = Object.assign({});
    },
    async handleSubmit() {
      let quantityM = 0;
      if (this.model.shelfGoodsNumList.length > 0) {
        this.model.shelfGoodsNumList.forEach((item) => {
          quantityM += Number(item.quantity);
        });
      }
      // 保存时候返回的list
      this.$emit(
        "ok",
        this.model.goodsId,
        quantityM,
        this.model.shelfGoodsNumList
      );
      this.visible = false;
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 1000;
      }
    },
  },
};
</script>

<style></style>
